#ifndef Animate_h__
#define Animate_h__
#include "Common.h"
#include "Render/Animate/Actor/act.h"
#include "Render/Animate/Actor/spr.h"
#include "DXlib/DXDraw.h"


struct RenderData
{
public:
	RenderData();
	int   state;
	int   direction;
	int   time;
	POINT point;
};


class Animate
{
protected:
	CSPR* m_pSpr;
	CACT* m_pAct;
	int   m_speed;
	int   m_elapsedTime;
	int   m_numFrame;
	int   m_curFrame;
	int   m_curAction;
    int   m_numSpriteInCurFrame;
	int   m_direction;
	int   m_curActionId;
	int   m_perFrameTime;
	POINT m_curPoint;
	bool  m_bIsEntity;

	Animate* m_pRefAnimate;
	LPDIRECTDRAWSURFACE* m_pSprSurfArry;

protected:
	int LoadFile(const char* fileName);
	int InitDXSurface();

public:
	void Update(const int deltaTime);
	void Draw(const int deltaTime);
	inline void SetRenderData(const RenderData& renderData);
	HRESULT ROBlt(LPDIRECTDRAWSURFACE destSurf,int sprId);
	CACT* GetAct();
	Animate(const char* fileName, Animate* pRefAnimate = NULL,bool bIsEntity = true);
	int Initialized(const char* fileName);
};

inline void Animate::SetRenderData(const RenderData& renderData)
{
	m_direction = renderData.direction;
	m_curPoint = renderData.point;

	//reset frame when action changed.
	if (m_curActionId != renderData.state)
	{
		m_curActionId = renderData.state;
		m_curFrame = 0;
	}

	m_curAction = m_direction + 8 * m_curActionId;
	m_numFrame = m_pAct->GetNumPattern(m_curAction);

	//don't support change the head's direction when stopped, or its head will be round and round...
	if (m_curActionId == 0 && m_bIsEntity)
	{
		m_numFrame = 1;
	}

	// may be this should be equal as renderData.time directly.
	m_perFrameTime = (renderData.time / m_numFrame) * 3;
}

#endif // Animate_h__